{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f46d6bf-02b5-4d7e-a602-833fb4f7d1cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.charts import Line\n",
    "from pyecharts.charts import Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "952b5f29-346e-4492-83a4-962b0061efd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#显示所有列\n",
    "pd.set_option('display.max_columns', None)\n",
    "#显示所有行\n",
    "pd.set_option('display.max_rows', None)\n",
    "#设置value的显示长度为100，默认为50\n",
    "pd.set_option('max_colwidth',100)\n",
    "df=pd.read_csv('WorldCupsSummary.csv',index_col=0)#将第一列作为索引列，即将时间作为索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0aec0adc-e847-4b40-8e83-677e88d44d24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                国家  冠军数  亚军数  季军数  第四名数    总数\n",
      "10         Germany  4.0  4.0  4.0   1.0  13.0\n",
      "3           Brazil  5.0  2.0  2.0   2.0  11.0\n",
      "12           Italy  4.0  2.0  1.0   1.0   8.0\n",
      "9           France  2.0  1.0  2.0   1.0   6.0\n",
      "22         Uruguay  2.0  0.0  0.0   3.0   5.0\n",
      "14     Netherlands  0.0  3.0  1.0   1.0   5.0\n",
      "0        Argentina  2.0  3.0  0.0   0.0   5.0\n",
      "19          Sweden  0.0  1.0  2.0   1.0   4.0\n",
      "8          England  1.0  0.0  0.0   2.0   3.0\n",
      "7   Czechoslovakia  0.0  2.0  0.0   0.0   2.0\n",
      "2          Belgium  0.0  0.0  1.0   1.0   2.0\n",
      "18           Spain  1.0  0.0  0.0   1.0   2.0\n",
      "16        Portugal  0.0  0.0  1.0   1.0   2.0\n",
      "15          Poland  0.0  0.0  2.0   0.0   2.0\n",
      "1          Austria  0.0  0.0  1.0   1.0   2.0\n",
      "11         Hungary  0.0  2.0  0.0   0.0   2.0\n",
      "6          Croatia  0.0  1.0  1.0   0.0   2.0\n",
      "23      Yugoslavia  0.0  0.0  0.0   2.0   2.0\n",
      "4         Bulgaria  0.0  0.0  0.0   1.0   1.0\n",
      "13  Korea Republic  0.0  0.0  0.0   1.0   1.0\n",
      "17    Soviet Union  0.0  0.0  0.0   1.0   1.0\n",
      "5            Chile  0.0  0.0  1.0   0.0   1.0\n",
      "20          Turkey  0.0  0.0  1.0   0.0   1.0\n",
      "21             USA  0.0  0.0  1.0   0.0   1.0\n"
     ]
    }
   ],
   "source": [
    "# 国家获得冠军数量\n",
    "groupbyed = df.groupby(['Winner']).groups\n",
    "for i in groupbyed:\n",
    "    groupbyed[i] = len(groupbyed[i])\n",
    "groupbyed['Germany'] = groupbyed['Germany FR'] + groupbyed['Germany']  # 合并Germany与Germany FR\n",
    "del groupbyed['Germany FR']\n",
    "groupbyed = pd.DataFrame([groupbyed]).T\n",
    "groupbyed.columns = ['nums']\n",
    "\n",
    "# 获得亚军数量\n",
    "Second = df.groupby('Second').groups\n",
    "for i in Second:\n",
    "    Second[i] = len(Second[i])\n",
    "Second['Germany'] = Second['Germany FR'] + Second['Germany']\n",
    "del Second['Germany FR']\n",
    "Second = pd.DataFrame([Second]).T\n",
    "Second.columns = ['nums']\n",
    "\n",
    "# 获得季军数\n",
    "Third = df.groupby('Third').groups\n",
    "for i in Third:\n",
    "    Third[i] = len(Third[i])\n",
    "Third['Germany'] = Third['Germany FR'] + Third['Germany']\n",
    "del Third['Germany FR']\n",
    "Third = pd.DataFrame([Third]).T\n",
    "Third.columns = ['nums']\n",
    "\n",
    "# 第四名数\n",
    "Fourth = df.groupby('Fourth').groups\n",
    "for i in Fourth:\n",
    "    Fourth[i] = len(Fourth[i])\n",
    "Fourth['Germany'] = Fourth['Germany FR']\n",
    "del Fourth['Germany FR']\n",
    "Fourth = pd.DataFrame([Fourth]).T\n",
    "Fourth.columns = ['nums']\n",
    "\n",
    "groupbyed.reset_index(inplace=True)\n",
    "Second.reset_index(inplace=True)\n",
    "Third.reset_index(inplace=True)\n",
    "Fourth.reset_index(inplace=True)\n",
    "\n",
    "# 使用suffixes参数避免列名冲突\n",
    "groupbyed = pd.merge(groupbyed, Second, how='outer', on='index', suffixes=('_winner', '_second'))\n",
    "groupbyed = pd.merge(groupbyed, Third, how='outer', on='index', suffixes=('_second', '_third'))\n",
    "groupbyed = pd.merge(groupbyed, Fourth, how='outer', on='index', suffixes=('_third', '_fourth'))\n",
    "\n",
    "# 更新列名\n",
    "groupbyed.columns = ['国家', '冠军数', '亚军数', '季军数', '第四名数']\n",
    "\n",
    "groupbyed.fillna(0, inplace=True)\n",
    "groupbyed['总数'] = groupbyed['冠军数'] + groupbyed['亚军数'] + groupbyed['季军数'] + groupbyed['第四名数']\n",
    "groupbyed.sort_values(by='总数', inplace=True, ascending=False)\n",
    "print(groupbyed)\n",
    "\n",
    "# 绘制图表的代码保持不变，这里只展示Bar图的代码作为示例\n",
    "c = (\n",
    "    Bar(init_opts=opts.InitOpts(width='1500px'))\n",
    "    .add_xaxis(list(groupbyed['国家']))\n",
    "    .add_yaxis(\"冠军数\", list(groupbyed['冠军数']), category_gap='15%')\n",
    "    .add_yaxis(\"亚军数\", list(groupbyed['亚军数']), category_gap='15%')\n",
    "    .add_yaxis(\"季军数\", list(groupbyed['季军数']), category_gap='15%')\n",
    "    .add_yaxis(\"第四名数\", list(groupbyed['第四名数']), category_gap='15%')\n",
    "    .add_yaxis('总数', list(groupbyed['总数']), category_gap='15%')\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"按照获奖总数排序\", pos_left='20%'),\n",
    "                     xaxis_opts=opts.AxisOpts(name='国家', axispointer_opts={'interval': '0'},\n",
    "                                              axislabel_opts=opts.LabelOpts(rotate=35, font_size=12)),\n",
    "                     yaxis_opts=opts.AxisOpts(name='数量'),\n",
    "                     legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(font_size=15)))\n",
    "    .render(\"前四名.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c29f1c70-1d8c-4de8-8f20-07512a5129eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      是否举办\n",
      "1942     0\n",
      "1946     0\n"
     ]
    }
   ],
   "source": [
    "#统计未举办年份\n",
    "Year=list(df.index)\n",
    "count={}\n",
    "for i in range(1930,2019,4):\n",
    "    count[str(i)]=Year.count(i)\n",
    "count=pd.DataFrame([count]).T\n",
    "count.columns=['是否举办']\n",
    "print(count[count['是否举办']==0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b59a1768-1dce-42be-92f9-5d7ddedffa11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\py\\\\jupyter\\\\场均进球.html'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GoalsScored=df.loc[:,'GoalsScored']\n",
    "changjun=np.array(GoalsScored)/np.array(df.loc[:,'MatchesPlayed'])\n",
    "changjun=[round(i,1) for i in changjun]\n",
    "# print(changjun)\n",
    "bar = (\n",
    "    Bar(init_opts=opts.InitOpts(width='1500px'))\n",
    "    .add_xaxis(list(GoalsScored.index))\n",
    "    .add_yaxis(\"总进球数\", GoalsScored.tolist(),category_gap='15%',z=0)\n",
    "    .add_yaxis('总参赛队伍数',list(df.loc[:,'QualifiedTeams']),category_gap='15%',z=0)\n",
    "    .add_yaxis(\"总比赛场数\",list(df.loc[:,'MatchesPlayed']),category_gap='15%',z=0)\n",
    "    .set_global_opts(\n",
    "                     xaxis_opts=opts.AxisOpts(axispointer_opts={'interval':'0'},axislabel_opts=opts.LabelOpts(rotate=35,font_size=12),name='Time'),\n",
    "                     yaxis_opts=opts.AxisOpts(name='Numbers'),\n",
    "                     legend_opts=opts.LegendOpts(textstyle_opts=opts.TextStyleOpts(font_size=15))))\n",
    "line=(\n",
    "    Line(init_opts=opts.InitOpts(width='1500px'))\n",
    "        .add_xaxis(GoalsScored.index.tolist())\n",
    "        .add_yaxis(\"场均进球数\",y_axis=changjun,is_smooth=True,is_symbol_show=True)\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"折线图-基本示例\"))\n",
    ")\n",
    "bar.overlap(line)\n",
    "bar.render('场均进球.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "aff36aea-5747-4835-b350-446dfc375481",
   "metadata": {},
   "outputs": [],
   "source": [
    "people=[round(i,2) for i in df.loc[:,'Attendance']/10000]\n",
    "c = (\n",
    "    Line(init_opts=opts.InitOpts(width='1150px'))\n",
    "    .add_xaxis(df.index.tolist())\n",
    "    .add_yaxis(\"现场观众总人数\", people, is_smooth=True,\n",
    "               markpoint_opts=opts.MarkPointOpts(data=[opts.MarkLineItem(type_='max',symbol_size = [80,50],name='max'),opts.MarkLineItem(type_='min',symbol_size = [80,50],name='min')]))\n",
    "    .set_global_opts(\n",
    "                     tooltip_opts=opts.TooltipOpts(\n",
    "                         is_show=True, trigger=\"axis\", axis_pointer_type=\"cross\"\n",
    "                     ),\n",
    "                     xaxis_opts=opts.AxisOpts(\n",
    "                         name='Time',\n",
    "                         type_=\"category\",\n",
    "                         axispointer_opts=opts.AxisPointerOpts(is_show=False, type_=\"shadow\"),\n",
    "                     ),\n",
    "                     yaxis_opts=opts.AxisOpts(\n",
    "                         name='numbers(10000)'\n",
    "                     ))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False)\n",
    "                     )\n",
    "    .render(\"现场观众总人数.html\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7edbf4ab-5f2d-48be-b53a-ab3190bb7018",
   "metadata": {},
   "outputs": [],
   "source": [
    "HostCountry=df.groupby(df.loc[:,'HostCountry']).groups\n",
    "for i in HostCountry:\n",
    "    HostCountry[i]=len(HostCountry[i])\n",
    "HostCountry['Korea']=1\n",
    "HostCountry['Japan']=1\n",
    "del HostCountry['Korea/Japan']\n",
    "HostCountry['United States']=HostCountry['USA']\n",
    "del HostCountry['USA']\n",
    "HostCountry['United Kingdom']=HostCountry['England']\n",
    "del HostCountry['England']\n",
    "HostCountry=[[i,HostCountry[i]] for i in HostCountry]\n",
    " \n",
    "c=(\n",
    "    Map(init_opts=opts.InitOpts(width='1150px'))\n",
    "        .add(\n",
    "            series_name=\"举办国家\",\n",
    "            data_pair=HostCountry,\n",
    "            maptype=\"world\",\n",
    "        )\n",
    "        # 全局配置项\n",
    "        .set_global_opts(\n",
    "            # 设置标题\n",
    "            title_opts=opts.TitleOpts(title=\"世界地图\"),\n",
    "            # 设置标准显示\n",
    "            visualmap_opts=opts.VisualMapOpts(max_=2, is_piecewise=False),\n",
    "        )\n",
    "        # 系列配置项\n",
    "        .set_series_opts(\n",
    "            # 标签名称显示，默认为True\n",
    "            label_opts=opts.LabelOpts(is_show=False, color=\"blue\"),showLegendSymbol=False\n",
    "        )\n",
    "        # 生成本地html文件\n",
    "        .render(\"世界地图.html\")\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9edd7523-b621-4198-9d21-61ba133e5a18",
   "metadata": {},
   "outputs": [],
   "source": [
    "Winner=df.groupby(['Winner']).groups\n",
    "for i in Winner :\n",
    "    Winner[i]=len(Winner[i])\n",
    "Winner['Germany']=Winner['Germany FR']+Winner['Germany']\n",
    "del Winner['Germany FR']\n",
    "Winner['United Kingdom']=Winner['England']\n",
    "del Winner['England']\n",
    "Winner=[[i,Winner[i]] for i in Winner]\n",
    "# Winner.columns=['nums']\n",
    "c=(\n",
    "    Map(init_opts=opts.InitOpts(width='1150px'))\n",
    "        .add(\n",
    "            series_name=\"夺冠国家\",\n",
    "            data_pair=Winner,\n",
    "            maptype=\"world\",\n",
    "        )\n",
    "        # 全局配置项\n",
    "        .set_global_opts(\n",
    "            # 设置标题\n",
    "            title_opts=opts.TitleOpts(title=\"世界地图\"),\n",
    "            # 设置标准显示\n",
    "            visualmap_opts=opts.VisualMapOpts(max_=5, is_piecewise=True),\n",
    "        )\n",
    "        # 系列配置项\n",
    "        .set_series_opts(\n",
    "            # 标签名称显示，默认为True\n",
    "            label_opts=opts.LabelOpts(is_show=False, color=\"blue\"),showLegendSymbol=False\n",
    "        )\n",
    "        # 生成本地html文件\n",
    "        .render(\"夺冠国家分布.html\")\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ea03f9a-0a2d-460c-96c4-de9807719ed7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
